* Reset settings and initialize log file
launch, path("share/summer_layoffs")

*-------------------------------------------------------------------------------
* Price and Wasserman (2024), "The Summer Drop in Female Employment"
*
* Description: Calculate sectoral contributions to temporary summer layoffs.
*-------------------------------------------------------------------------------


* Prepare data and run models
*-------------------------------------------------------------------------------

if "$estimate" != "0" {
	* Load estimation sample
	gzuse "$basepath/data/derived/cps_bms_sample.dta.gz", clear

	* Designate child day care and bus services as education-adjacent
	gen byte adjacent = inlist(ind1990, 401, 862)

	* Tag workers on temporary layoff, overall and by sector
	gen byte temploser = (unemp == 1 & whyunemp == 1)
	gen byte tempschool = (temploser == 1 & school == 1)
	gen byte tempedadj = (temploser == 1 & adjacent)
	gen byte tempother = (temploser == 1 & tempschool == 0 & tempedadj == 0)

	* Compute shares of adults on temporary layoff
	gcollapse (mean) temp* (rawsum) wtfinl (first) month tmspline* weeks [pw = wtfinl], by(female tm)
	tsset female tm

	* Run specifications and save the estimates to disk
	foreach f of numlist 0 1 {
		foreach yvar of varlist temploser tempschool tempedadj tempother {
			quietly ivreg2 `yvar' ib5.month tmspline* weeks if female == `f' [aw = wtfinl], bw($bandwidth) robust small
			process_estimates, path("summer_layoffs") model("f`f'_`yvar'")
		}
	}
}


* Prepare estimates
*-------------------------------------------------------------------------------

* Load estimates into memory
load_estimates, path("summer_layoffs")

* Prepare coefficient labels for each month
make_coeflabels


* Plot seasonality in temporary layoffs
*-------------------------------------------------------------------------------

* Extract estimates
tempfile coefs
capture postclose coefs
postfile coefs byte female str10 group byte month_adj float beta using `coefs'

foreach f of numlist 0 1 {
	foreach g in "loser" "school" "edadj" "other" {
		estimates restore f`f'_temp`g'

		foreach m of numlist 0/12 {
			post coefs (`f') ("`g'") (`m') (_b[coef`m'])
		}
	}
}

postclose coefs
use `coefs', clear

replace beta = 100 * beta
reshape wide beta, i(female month_adj) j(group) string
rename beta* *

label define month_adj_lbl  0 "May", replace
label define month_adj_lbl  1 "June", add
label define month_adj_lbl  2 "July", add
label define month_adj_lbl  3 "Aug.", add
label define month_adj_lbl  4 "Sept.", add
label define month_adj_lbl  5 "Oct.", add
label define month_adj_lbl  6 "Nov.", add
label define month_adj_lbl  7 "Dec.", add
label define month_adj_lbl  8 "Jan.", add
label define month_adj_lbl  9 "Feb.", add
label define month_adj_lbl 10 "Mar.", add
label define month_adj_lbl 11 "Apr.", add
label define month_adj_lbl 12 "May", add
label values month_adj month_adj_lbl

* Bar positioning
gen curr_pos = 0
gen curr_neg = 0

foreach v in "school" "edadj" "other" {
	gen `v'0 = .
	replace `v'0 = curr_pos if sign(`v') >= 0
	replace `v'0 = curr_neg if sign(`v') < 0

	gen `v'1 = `v'0 + `v'
	replace curr_pos = curr_pos + `v' if `v' > 0
	replace curr_neg = curr_neg + `v' if `v' < 0
}

drop curr_*

* Prepare background shading
gen rlow = -.2
gen rupp = 1
gen rval = month_adj - 0.5

* Plot estimates
foreach f of numlist 0 1 {
	if `f' == 0 local flbl "Men"
	if `f' == 1 local flbl "Women"

	#delimit ;
	twoway
		(rarea rupp rlow rval if female == `f' & inrange(month_adj, 0, 5), color($ltgs) plotregion(margin(t=0 b=0 l=0)))
		(rbar school0 school1 month_adj if female == `f', color($col1) fcolor(*1.20) lcolor(black) lwidth(vthin))
		(rbar edadj0 edadj1 month_adj if female == `f', color($col2) fcolor(*0.8) lcolor(black) lwidth(vthin))
		(rbar other0 other1 month_adj if female == `f', color($col3) fcolor(*0.6) lcolor(black) lwidth(vthin))
		(connected loser month_adj if female == `f', color(black) msymbol(circle) clwidth(medthick))
		(scatteri 0 -0.5 0 12.5, recast(line) color(black)),
		title("{bf:`flbl'}")
		xtitle("")
		xscale(range(0.5 12.5))
		xlabel(0 "May" 1 "June" 2 "July" 3 "Aug." 4 "Sept." 5 "Oct." 6 "Nov." 7 "Dec." 8 "Jan." 9 "Feb." 10 "Mar." 11 "Apr." 12 "May", alternate)
		ytitle("")
		yscale(range(-.2 1))
		ylabel(-.2(.2)1)
		yline(0)
		graphregion(margin(small))
		plotregion(margin(zero))
		legend(rows(1) size(*0.8) order(5 2 3 4)
			label(2 "Education sector")
			label(3 "Education-adjacent")
			label(4 "Other sectors")
			label(5 "Total"));
	#delimit cr

	tempfile g`f'
	graph save "`g`f''"
}

* Report coefficients
esttab f1_temploser f1_tempschool f1_tempedadj, keep(coef2) transform(@*100)

foreach g in loser school edadj {
	estimates restore f1_temp`g'
	local `g' = _b[coef2]
}

display "Education sector as share of total: " %9.2f `=100 * (`school'/`loser')'
display "Ed. + ed-adjacent as share of total: " %9.2f `=100 * ((`school' + `edadj')/`loser')'

* Create pdf file
grc1leg "`g1'" "`g0'", l1title("      Difference relative to May (p.p.)", size(*.8)) imargin(vsmall)
nicepdf "$basepath/output/summer_layoffs.pdf", panels(2) cropbuffer(2) indirect replace

* Close the log file
unlaunch
